<template>
    <div class="ft_dialog">
      <div class="dialog_bg" v-show="visibleDialog"></div>
      <div class="dialog_box" v-show="visibleDialog">
          <ft_button v-show="!visibleAnswer" :handleFn="showAnswer" :title="title"></ft_button>
          <ft_input v-show="visibleAnswer" v-model="answerValue"></ft_input>
          <ft_button class="dialog_submit"  v-show="visibleAnswer" :handleFn="handleSubmit" :title="answerTitle"></ft_button>
      </div>
    </div>
</template>

<script>
import ftButton from '@/components/FtButton/ft_button'
import ftInput from '@/components/FtInput/ft_input'
import { message } from 'element-ui'

export default {
  components: {
    'ft_button': ftButton,
    'ft_input': ftInput
  },
  data () {
    return {
      visibleDialog: true, // 显示dialog
      visibleAnswer: false, // 显示提问框

      answerValue: ''
    }
  },
  created () {
    this.visibleDialog = this.visible
  },
  props: {
    title: {
      type: String,
      default: '你今天想解决什么问题？'
    },
    answerTitle: {
      type: String,
      default: '确定'
    },
    visible: {
      type: Boolean,
      default: true
    },
    handleFn: { // 必须有返回值
      type: Function,
      default: function () {}
    }
  },
  methods: {
    showAnswer () {
      this.visibleAnswer = true
    },
    handleSubmit () {
      let promise = this.handleFn(this.answerValue)
      if (typeof promise === 'undefined') {
        this.$message({
          message: '错误的返回值',
          type: 'warning'
        })
        return
      }
      promise ? this.visibleDialog = false : message('请求错误，请重试')
    }
  }
}
</script>

<style lang="less" scoped>
@import './../../assets/globalStyles/index.less';
.ft_dialog {
  .dialog_bg {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 2;

    background-color: @defaultDialogBgColor;
  }
  .dialog_box {
    position: absolute;
    width: auto;
    height: auto;
    top: 35%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 3;

    display: flex;
    align-items: center;
    justify-content: space-between;

    .dialog_submit {
      margin-left: @defaultButtonSpace;
    }
  }
}
</style>
